<?php
    $run_key = "c4f1a5859562bef95b539b84a87aaddd";
    
    $key_jcdecaux = "9ae1d3d91b14651ee0bf52d4b3194ac64ff95c4c";
    $url_jcdecaux = "https://api.jcdecaux.com/vls/v1/stations?contract=Paris&apiKey=";
    $tmp_key = null;
    if(isset($argv)){
        $tmp_key = $argv[1];
    }
    if(isset($_GET)){
        $tmp_key = $_GET["key"];
    }
    if(($tmp_key==$run_key||$tmp_key==$run_key)&&!isset($_GET["maj_parc"])){
        require_once 'app/includes/propel.php';
        $contents = file_get_contents($url_jcdecaux.$key_jcdecaux);
        $contents = utf8_encode($contents);
        $results = json_decode($contents);
        //var_dump($results);
        $error = array();
        $maj = 0;
        for($i=0;$i<count($results);$i++){
            $result = $results[$i];
            //var_dump($result);
            $date = new DateTime();
            $date->setTimestamp($result->last_update/1000);
            $query = TabInformationStationQuery::create()->filterByFkStation($result->number)->filterByLastUpdate($date)->findOne();
            if($query==null){
                $maj++;
                $info = new TabInformationStation();
                $info->setFkStation($result->number);
                $info->setAvailableBikeStands($result->available_bike_stands);
                $info->setAvailableBikes($result->available_bikes);
                $info->setBanking($result->banking);
                $info->setBonus($result->bonus);
                $info->setLastUpdate($date);
                $info->setBikeStands($result->bike_stands);
                if($result->status=="OPEN"){
                    $status = true;
                }
                else{
                    $status = false;
                }
                $info->setStatusStation($status);
                try {
                    $info->save();
                    
                    
                    if($info->getAvailableBikeStands()/$info->getBikeStands() > 0.95){
                        $repartition = TabRepartitionQuery::create()->filterByFkStation($info->getFkStation())->filterByStatut(0)->findOne();
                        if($repartition == null){
                            $repartition = new TabRepartition();
                            $repartition->setFkStation($info->getFkStation());
                            $repartition->setStatut(0);
                            $repartition->setDateRepartition(new DateTime());
                            $repartition->save();
                        }
                    }
                    else{
                        $repartition = TabRepartitionQuery::create()->filterByFkStation($info->getFkStation())->filterByStatut(0)->findOne();
                        if($repartition != null){
                            $repartition->setStatut(1);
                            $repartition->save();
                        }
                    }
                }
                catch(Exception $error_string){
                    $error[] = $result;
                }
                
            }
        }
        echo "<b>Nombre d'informations station récupérées : </b>".count($results)."<br />";
        echo "<b>Nombre d'informations mises à jour : </b>".($maj-count($error))."<br />";
        echo "<b>Nombre d'erreurs : </b>".count($error)."<br />";
        echo "<b>Liste des erreurs : </b><br />";
        var_dump($error);
    }
    
    if(isset($_GET["maj_parc"])){
        if($tmp_key==$run_key||$tmp_key==$run_key){
            require_once 'app/includes/propel.php';
            $stations = TabStationQuery::create()->find();
            foreach ($stations as $station){
                $info = TabInformationStationQuery::create()->filterByFkStation($station->getIdStation())->orderByLastUpdate(Criteria::DESC)->findOne();
                if($info!=null){
                    $byciclettes = TabBycicletteQuery::create()->filterByFkStation($station->getIdStation())->filterByStatusByciclette(0)->find();
                    if($info->getAvailableBikes()>$byciclettes->count()){
                        for($i=0;$i<$info->getAvailableBikes()-$byciclettes->count();$i++){
                            $byciclette = new TabByciclette();
                            $byciclette->setFkStation($station->getIdStation());
                            $byciclette->setStatusByciclette(0);
                            $byciclette->save();
                        }
                    }
                    if($info->getAvailableBikes()<$byciclettes->count()){
                        for($i=$info->getAvailableBikes();$i<$byciclettes->count();$i++){
                            $byciclette = $byciclettes->get($i);
                            $byciclette->delete();
                        }
                    }
                    $byciclettes_hs = TabBycicletteQuery::create()->filterByFkStation($station->getIdStation())->filterByStatusByciclette(1)->find();
                    if($byciclettes_hs->count()<$info->getBikeStands()-$info->getAvailableBikeStands()-$info->getAvailableBikes()){
                        for($i=$info->getAvailableBikeStands()+$info->getAvailableBikes();$i<$info->getBikeStands();$i++){
                            $byciclette = new TabByciclette();
                            $byciclette->setFkStation($station->getIdStation());
                            $byciclette->setStatusByciclette(1);
                            $byciclette->save();
                            $byciclette->reload();
                            $reparation = new TabReparation();
                            $reparation->setFkByciclette($byciclette->getIdByciclette());
                            $reparation->setStatut(0);
                            $reparation->setDateReparation(new DateTime());
                            $reparation->save();
                        }
                    }
                    if($byciclettes_hs->count()>$info->getBikeStands()-$info->getAvailableBikeStands()-$info->getAvailableBikes()){
                        for($i=0;$i<$info->getBikeStands()-$info->getAvailableBikeStands()-$info->getAvailableBikes();$i++){
                            $byciclette = $byciclettes_hs->get($i);
                            $byciclette->delete();
                        }
                    }
                }
            }
        }
    }
    
?>
